package com.example.day_blog.utils;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author starsea
 * @date 2024-01-28 16:10
 */
@RestController
public class SecurityUtils {
    public static String encry(String password)
    {
        //生成随机盐值
        String salt= UUID.randomUUID().toString().replace("-","");
        System.out.println(salt);
        //加密 盐值加明文
        String securityPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        //数据库中存储盐值加铭文
        return salt+securityPassword;
    }
    //校验
    public static boolean verify(String inputPassword,String sqlPassword)
    {
        //去除盐值
        if(sqlPassword==null || sqlPassword.length()!=64)
        {
            return false;
        }
        //得到密文
        String salt=sqlPassword.substring(0,32);
        String securityPassword=DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes());
        return (salt+securityPassword).equals(sqlPassword);
    }
}
